<?php

include 'form_model.php';
include 'form_questions_data.php';
require ('xajax/xajax_core/xajax.inc.php'); //include ajax class

$xajax = new xajax();
$xajax->setCharEncoding('UTF-8');
$xajax->configure('decodeUTF8Inp',true);
$xajax->configure('debug', true);


/* function called after each validation */
function process_form_val($form_values){
	
	$lang=$form_values[lang];
	
	$answer = new xajaxResponse();  //Instance of xajax response
	
	switch ($form_values[chance]) {
		case 0 :
			$mail_verification=mail_exists($form_values[email]);
			if($mail_verification==false){
				$output=get_questions('1',$lang,$form_values);
			}
			else {
				$form_values[verified_mail]=$mail_verification;
				$output = get_questions('R', $lang,$form_values);
			}
			//Show the output in the element with name basic_data
   			$answer->assign("form_content","innerHTML",$output);
   			
		break; //End of case 0
			
			case 9:
			if(getStep($form_values[id],'birth_city')==NULL){
				$output= get_questions('2', $lang,$form_values);
				}
			elseif(getStep($form_values[id],'brief_description')==NULL){
				$output= get_questions('3', $lang,$form_values);
				}
			elseif(getStep($form_values[id],'competitive_person')==NULL){
				$output= get_questions('4', $lang,$form_values);
				}
			elseif(getStep($form_values[id],'exciting_moment')==NULL){
				$output= get_questions('5', $lang,$form_values);
				}
			elseif(getStep($form_values[id],'travel_problems')==NULL){
				$output= get_questions('6', $lang,$form_values);
				}
			else {
				$output= get_questions('9', $lang,$form_values);
				//$output="ZERO";
				}
			//$output=getStep($form_values[id],'birth_city');
			$answer->assign("form_content","innerHTML",$output);
			break;
			
			case 1:
			$pass=$form_values['password'];
			$output=get_user_data($form_values[id], $pass);
			$answer->assign("form_content","innerHTML",$output);
			break; //End of case 1
			
			case 2:
				$fecha=strftime( "%Y-%m-%d", time() );
				foreach($form_values as $valor){
					$valor=nosql($valor);
					$form_values[]=$valor;
					}
				$query="INSERT INTO suscribers (fecha_creacion, nombres, apellidos, email, `password`, pais_nacimiento, genero, idioma_nativo, otros_idiomas, pais_residencia, estado_residencia, ciudad_residencia, telefono_contacto) VALUES ('".$fecha."','".$form_values[name]."','".$form_values[last_name]."','".$form_values[email]."', '".md5($form_values[password].get_safe_salt())."','".$form_values[native_country]."','".$form_values[gender]."','".$form_values[language]."','".$form_values[other_languages]."','".$form_values[residence_country]."','".$form_values[residence_state]."','".$form_values[residence_city]."','".$form_values[contact_phone]."')";
				$form_values[last_id]=write($query);
				$output= get_questions('2', $lang,$form_values);
				$answer->assign("form_content","innerHTML",$output);
			break; //End of case 1
			
			case 3:
				foreach($form_values as $valor){
					$valor=nosql($valor);
					$form_values[]=$valor;
					}
				$query="UPDATE suscribers SET birth_city='".$form_values[birth_city]."', birth_date='".$form_values[birth_date]."', work_phone='".$form_values[work_phone]."',cellphone='".$form_values[cellphone]."', marital_status='".$form_values[marital_status]."', number_of_childs='".$form_values[number_of_childs]."', name_and_age_of_childs='".$form_values[name_and_age_childs]."', emergency_contact='".$form_values[emergency_contact]."', study_level='".$form_values[study_level]."', current_ocupation='".$form_values[current_ocupation]."' WHERE id='".$form_values[id]."' LIMIT 1";
				write($query);
				$output= get_questions('3', $lang,$form_values);
				$answer->assign("form_content","innerHTML",$output);
			break;
			
			case 4:
				foreach($form_values as $valor){
						$valor=nosql($valor);
						$form_values[]=$valor;
						}
				$query="UPDATE suscribers SET brief_description='".$form_values[brief_description]."',places_where_lived='".$form_values[places_where_lived]."',considered_traveler='".$form_values[considered_traveler]."',why_participate='".$form_values[why_participate]."',has_visited_colombia='".$form_values[has_visited_colombia]."',how_many_times='".$form_values[how_many_times]."',what_know='".$form_values[what_know]."',know_admire='".$form_values[know_admire]."',who_why='".$form_values[who_why]."',opinion='".$form_values[opinion]."',what_attract='".$form_values[what_attract]."',what_like='".$form_values[what_like]."',what_not_like='".$form_values[what_not_like]."',how_frequently='".$form_values[how_frequently]."',kind_destinations='".$form_values[kind_destinations]."',kind_activities='".$form_values[kind_activities]."',left_country='".$form_values[left_country]."',worst_experience='".$form_values[worst_experience]."',best_experience='".$form_values[best_experience]."',your_life_experience='".$form_values[your_life_experience]."' WHERE id='".$form_values[id]."' LIMIT 1";
				write($query);
				$output= get_questions('4', $lang,$form_values);
				$answer->assign("form_content","innerHTML",$output);
			break;
			
			case 5:
				foreach($form_values as $valor){
					$valor=nosql($valor);
					$form_values[]=$valor;
					}
				$query="UPDATE suscribers SET competitive_person='".$form_values[competitive_person]."',competitive_why='".$form_values[competitive_why]."',teamwork='".$form_values[teamwork]."',sports='".$form_values[sports]."',skills='".$form_values[skills]."',hobbies='".$form_values[hobbies]."',musical_instrument='".$form_values[musical_instrument]."',personality_trait='".$form_values[personality_trait]."',physical_skills='".$form_values[physical_skills]."',advantages_disadvantages_women='".$form_values[advantages_disadvantages_women]."',advantages_disadvantages_man='".$form_values[advantages_disadvantages_man]."',good_representative='".$form_values[good_representative]."' WHERE id='".$form_values[id]."' LIMIT 1";
				write($query);
				$output=get_questions('5', $lang,$form_values);
				$answer->assign("form_content","innerHTML",$output);
			break;
				
			case 6:
				foreach($form_values as $valor){
					$valor=nosql($valor);
					$form_values[]=$valor;
					}
				$query="UPDATE suscribers SET exciting_moment='".$form_values[exciting_moment]."',three_adjectives='".$form_values[three_adjectives]."',pride='".$form_values[pride]."',shame='".$form_values[shame]."',angry='".$form_values[angry]."',laugh='".$form_values[laugh]."',cry='".$form_values[cry]."',time='".$form_values[time]."',lottery='".$form_values[lottery]."',drive_manual_car='".$form_values[drive_manual_car]."',drive_automatic_car='".$form_values[drive_automatic_car]."',drive_motorcycle='".$form_values[drive_motorcycle]."',swim='".$form_values[swim]."',diving='".$form_values[diving]."',run='".$form_values[run]."',use_videocamera='".$form_values[use_videocamera]."',take_photos='".$form_values[take_photos]."',dance='".$form_values[dance]."',sing='".$form_values[sing]."',talk_to_strangers='".$form_values[talk_to_strangers]."' WHERE id='".$form_values[id]."' LIMIT 1";
				write($query);
				$output=get_questions('6', $lang,$form_values);
				$answer->assign("form_content","innerHTML",$output);
			break;
			
			case 7:
				foreach($form_values as $valor){
					$valor=nosql($valor);
					$form_values[]=$valor;
					}
				$query="UPDATE suscribers SET travel_problems='".$form_values[travel_problems]."',allergies='".$form_values[allergies]."',phobias='".$form_values[phobias]."', illness='".$form_values[illness]."',medicines='".$form_values[medicines]."',law_trouble='".$form_values[law_trouble]."',visa_denied='".$form_values[visa_denied]."',tv_show='".$form_values[tv_show]."' WHERE id='".$form_values[id]."' LIMIT 1";
				write($query);
				$output=get_questions('7', $lang,$form_values);
				$answer->assign("form_content","innerHTML",$output);
			break;
				
	}

	return $answer; //return the ajax response instance
}

/* Function for prevent the SQL Injection*/
function nosql($var){
		if(get_magic_quotes_gpc())
		{$value = stripslashes($value);}
		return $value;
	}
   
?>
